package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //球会落何处
    public int[] findBall(int[][] grid) {
        int n = grid.length, m = grid[0].length;
        int[] res = new int[m];
        for (int i = 0; i < m; i++) {
            int x = 0, y = i;
            // 1 留意右边， -1 留意左边
            while (x < n) {
                int cur = grid[x][y], next = -grid[x][y];
                if(cur > 0) {
                    next = y == m - 1 ? -grid[x][y] : grid[x][y + 1];
                    y++;
                } else {
                    next = y == 0 ? -grid[x][y] : grid[x][y - 1];
                    y--;
                }
                if(cur * next < 0) {
                    break;
                }
                x++;
            }
            res[i] = x == n ? y : -1;
        }
        return res;
    }
}
